package org.train.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.annotation.*;
import org.train.entity.ProjectPo;
import org.train.entity.ReportPo;
import org.train.util.R;
import org.train.service.ProjectService;
import org.train.util.RUtil;
import org.train.util.Renum;

import java.util.List;

@RestController
@RequestMapping("/project")
public class ProjectController{
    @Autowired
    private ProjectService projectService;

    // 查询所有项目的简略信息
    @GetMapping("/queryAllInfos")
    public R queryAllProjectInfos(Integer pageNum, Integer pageSize, String search){
        return projectService.queryAllInfos(pageNum, pageSize, search);
    }

    // 查询总计栏
    @GetMapping("/queryTotalInfo")
    public R queryTotalInfo(){
        return projectService.queryTotalInfo();
    }

    // 查询单个项目的详细信息
    @GetMapping("/queryDetail")
    public R queryProjectDetail(@RequestParam Integer id){
        return projectService.queryDetail(id);
    }

    // 提交更改单个项目
    @PostMapping("/saveOrUpdateOne")
    public R saveOrUpdateOne(@RequestBody ProjectPo projectPo){
        if(projectPo.getContractName() == null
            || projectPo.getProjectLeader() == null
            || projectPo.getContractSigner() == null
            || projectPo.getContractAmount() == null
            || projectPo.getContractStartTime() == null
            || projectPo.getContractEndTime() == null
            || projectPo.getProjectYear() == null){
            return RUtil.error(Renum.REQUIRED_FIELDS_MISSING);
        }
        return projectService.saveOrUpdateOne(projectPo);
    }

    // 仅有项目id和项目名的信息
    @GetMapping("/queryAllBaseInfos")
    public R queryAllProjectBaseInfos(@RequestParam(required = false) Integer year){
        return projectService.queryAllBaseInfos(year);
    }

    @PostMapping("/removeBatch")
    public R removeBatch(@RequestBody List<Integer> ids){
        return projectService.removeBatchByUpdate(ids);
    }
}
